//
//  WithdrawAddressListViewController.m
//  AppleMarket
//
//  Created by yangy on 07/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "WithdrawAddressListViewController.h"
// model
#import "CoinAssetModel.h"
#import "AddressModel.h"
// controller
#import "NewAddressViewController.h"

@interface WithdrawAddressListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<AddressModel*> *dataList;
@end

@implementation WithdrawAddressListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = [NSString stringWithFormat:@"选择%@提现地址",self.assetModel.Unit];
    [self setNagivationBar];
    [self.view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void) setNagivationBar
{
    UIButton *rightButton = [UIButton cm_ButtonWithTitle:@"新增地址" font:kFont(13) titleColor:kColorWithHex(0x3291EB)];
    rightButton.frame = CGRectMake(0, 0, 88, 44);
    rightButton.titleLabel.textAlignment = NSTextAlignmentRight;
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NewAddressViewController *newVC = [[NewAddressViewController alloc] init];
        newVC.assetModel = self.assetModel;
        newVC.newCompleteHandler = ^{
            [self fetchAddreessList];
        };
        [self bh_PushViewController:newVC hiddenTabbar:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

#pragma mark - 获取提现地址列表
- (void) fetchAddreessList
{
    [NetRequest request_GetUserWithdrawAccountListWithCeId:self.assetModel.CurrencyID completeHandler:^(id  _Nonnull responseObj) {
        [self.tableView.mj_header endRefreshing];
        if([[responseObj objectForKey:@"Status"] boolValue]){
            NSArray *data = [responseObj objectForKey:@"Data"];
            self.dataList = [[data.rac_sequence map:^id _Nullable(id  _Nullable value) {
                return [AddressModel yy_modelWithDictionary:value];
            }] array];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.detailTextLabel.textColor = kColorWithHex(0x0E304C);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    AddressModel *m = self.dataList[indexPath.row];
    cell.textLabel.text = m.Memo;
    cell.detailTextLabel.text = m.BlockAddr;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressModel *m = self.dataList[indexPath.row];
    if(self.selectHandler){
        self.selectHandler(m);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorColor:kColorWithHex(0xEEF2F5)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setRowHeight:74];
        [_tableView setMj_header:[MJRefreshHeader headerWithRefreshingBlock:^{
            [self fetchAddreessList];
        }]];
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}

@end
